home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / Bright.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-05  |  8.1 KB  |  253 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmBright 
  4.    Caption         =   "Bright []"
  5.    ClientHeight    =   3375
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   5160
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   3375
  11.    ScaleWidth      =   5160
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.HScrollBar hbarBrightness 
  14.       Height          =   255
  15.       Left            =   720
  16.       Max             =   100
  17.       Min             =   -100
  18.       TabIndex        =   2
  19.       Top             =   120
  20.       Width           =   2415
  21.    End
  22.    Begin MSComDlg.CommonDialog dlgOpenFile 
  23.       Left            =   4560
  24.       Top             =   0
  25.       _ExtentX        =   847
  26.       _ExtentY        =   847
  27.       _Version        =   393216
  28.    End
  29.    Begin VB.PictureBox picOriginal 
  30.       AutoSize        =   -1  'True
  31.       Height          =   2775
  32.       Left            =   120
  33.       ScaleHeight     =   181
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   157
  36.       TabIndex        =   1
  37.       Top             =   480
  38.       Width           =   2415
  39.    End
  40.    Begin VB.PictureBox picResult 
  41.       Height          =   2775
  42.       Left            =   2640
  43.       ScaleHeight     =   181
  44.       ScaleMode       =   3  'Pixel
  45.       ScaleWidth      =   157
  46.       TabIndex        =   0
  47.       Top             =   480
  48.       Width           =   2415
  49.    End
  50.    Begin VB.Label lblBrighhtness 
  51.       Alignment       =   1  'Right Justify
  52.       BorderStyle     =   1  'Fixed Single
  53.       Caption         =   "0"
  54.       Height          =   255
  55.       Left            =   3960
  56.       TabIndex        =   5
  57.       Top             =   120
  58.       Width           =   495
  59.    End
  60.    Begin VB.Label Label1 
  61.       Caption         =   "Darker"
  62.       Height          =   255
  63.       Index           =   1
  64.       Left            =   120
  65.       TabIndex        =   4
  66.       Top             =   120
  67.       Width           =   615
  68.    End
  69.    Begin VB.Label Label1 
  70.       Caption         =   "Brighter"
  71.       Height          =   255
  72.       Index           =   0
  73.       Left            =   3240
  74.       TabIndex        =   3
  75.       Top             =   120
  76.       Width           =   615
  77.    End
  78.    Begin VB.Menu mnuFile 
  79.       Caption         =   "&File"
  80.       Begin VB.Menu mnuFileOpen 
  81.          Caption         =   "&Open..."
  82.          Shortcut        =   ^O
  83.       End
  84.       Begin VB.Menu mnuFileSaveAs 
  85.          Caption         =   "Save &As..."
  86.          Shortcut        =   ^A
  87.       End
  88.    End
  89. Attribute VB_Name = "frmBright"
  90. Attribute VB_GlobalNameSpace = False
  91. Attribute VB_Creatable = False
  92. Attribute VB_PredeclaredId = True
  93. Attribute VB_Exposed = False
  94. Option Explicit
  95. ' Arrange the controls.
  96. Private Sub ArrangeControls()
  97.     ' Position the result PictureBox.
  98.     picResult.Move _
  99.         picOriginal.Left + picOriginal.Width + 120, _
  100.         picOriginal.Top, _
  101.         picOriginal.Width, _
  102.         picOriginal.Height
  103.     picResult.Cls
  104.     ' This makes the image resize itself to
  105.     ' fit the picture.
  106.     picResult.Picture = picResult.Image
  107.     ' Make the form big enough.
  108.     Width = picResult.Left + picResult.Width + _
  109.         Width - ScaleWidth + 120
  110.     Height = picResult.Top + picResult.Height + _
  111.         Height - ScaleHeight + 120
  112.     DoEvents
  113. End Sub
  114. ' Transform the image.
  115. Private Sub TransformImage()
  116. Dim factor As Single
  117. Dim pixels() As RGBTriplet
  118. Dim bits_per_pixel As Integer
  119. Dim X As Integer
  120. Dim Y As Integer
  121.     ' Get the selected brightness value.
  122.     factor = hbarBrightness.value / 100#
  123.     ' Get the pixels from picOriginal.
  124.     GetBitmapPixels picOriginal, pixels, bits_per_pixel
  125.     ' Set the pixel colors.
  126.     For Y = 0 To picOriginal.ScaleHeight - 1
  127.         For X = 0 To picOriginal.ScaleWidth - 1
  128.             With pixels(X, Y)
  129.                 If factor < 0 Then
  130.                     ' Make the color darker.
  131.                     .rgbRed = (1 + factor) * .rgbRed
  132.                     .rgbGreen = (1 + factor) * .rgbGreen
  133.                     .rgbBlue = (1 + factor) * .rgbBlue
  134.                 Else
  135.                     ' Make the color brighter.
  136.                     .rgbRed = .rgbRed + factor * (255 - .rgbRed)
  137.                     .rgbGreen = .rgbGreen + factor * (255 - .rgbGreen)
  138.                     .rgbBlue = .rgbBlue + factor * (255 - .rgbBlue)
  139.                 End If
  140.             End With
  141.         Next X
  142.     Next Y
  143.     ' Set picResult's pixels.
  144.     SetBitmapPixels picResult, bits_per_pixel, pixels
  145.     picResult.Picture = picResult.Image
  146. End Sub
  147. ' Start in the current directory.
  148. Private Sub Form_Load()
  149.     picOriginal.AutoSize = True
  150.     picOriginal.ScaleMode = vbPixels
  151.     picOriginal.AutoRedraw = True
  152.     picResult.ScaleMode = vbPixels
  153.     picResult.AutoRedraw = True
  154.     dlgOpenFile.CancelError = True
  155.     dlgOpenFile.InitDir = App.Path
  156.     dlgOpenFile.Filter = _
  157.         "Bitmaps (*.bmp)|*.bmp|" & _
  158.         "GIFs (*.gif)|*.gif|" & _
  159.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  160.         "Icons (*.ico)|*.ico|" & _
  161.         "Cursors (*.cur)|*.cur|" & _
  162.         "Run-Length Encoded (*.rle)|*.rle|" & _
  163.         "Metafiles (*.wmf)|*.wmf|" & _
  164.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  165.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  166.         "All Files (*.*)|*.*"
  167. End Sub
  168. ' Transform the image.
  169. Private Sub hbarBrightness_Change()
  170.     lblBrighhtness.Caption = Format$(hbarBrightness.value)
  171.     ' If an image is loaded, transform it.
  172.     If picOriginal.Picture <> 0 Then
  173.         Screen.MousePointer = vbHourglass
  174.         DoEvents
  175.         TransformImage
  176.         Screen.MousePointer = vbDefault
  177.     End If
  178. End Sub
  179. ' Display the brightness value selected.
  180. Private Sub hbarBrightness_Scroll()
  181.     lblBrighhtness.Caption = Format$(hbarBrightness.value)
  182. End Sub
  183. ' Load the indicated file.
  184. Private Sub mnuFileOpen_Click()
  185. Dim file_name As String
  186.     ' Let the user select a file.
  187.     On Error Resume Next
  188.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  189.     dlgOpenFile.ShowOpen
  190.     If Err.Number = cdlCancel Then
  191.         Exit Sub
  192.     ElseIf Err.Number <> 0 Then
  193.         Beep
  194.         MsgBox "Error selecting file.", , vbExclamation
  195.         Exit Sub
  196.     End If
  197.     On Error GoTo 0
  198.     Screen.MousePointer = vbHourglass
  199.     DoEvents
  200.     file_name = Trim$(dlgOpenFile.FileName)
  201.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  202.         - Len(dlgOpenFile.FileTitle) - 1)
  203.     Caption = "Bright [" & dlgOpenFile.FileTitle & "]"
  204.     ' Open the original file.
  205.     On Error GoTo LoadError
  206.     picOriginal.Picture = LoadPicture(file_name)
  207.     On Error GoTo 0
  208.     ' Make picResult the same size and position it.
  209.     ArrangeControls
  210.     ' Transform the image.
  211.     TransformImage
  212.     Screen.MousePointer = vbDefault
  213.     Exit Sub
  214. LoadError:
  215.     Screen.MousePointer = vbDefault
  216.     MsgBox "Error " & Format$(Err.Number) & _
  217.         " opening file '" & file_name & "'" & vbCrLf & _
  218.         Err.Description
  219. End Sub
  220. ' Save the transformed image.
  221. Private Sub mnuFileSaveAs_Click()
  222. Dim file_name As String
  223.     ' Let the user select a file.
  224.     On Error Resume Next
  225.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  226.     dlgOpenFile.ShowSave
  227.     If Err.Number = cdlCancel Then
  228.         Exit Sub
  229.     ElseIf Err.Number <> 0 Then
  230.         Beep
  231.         MsgBox "Error selecting file.", , vbExclamation
  232.         Exit Sub
  233.     End If
  234.     On Error GoTo 0
  235.     Screen.MousePointer = vbHourglass
  236.     DoEvents
  237.     file_name = Trim$(dlgOpenFile.FileName)
  238.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  239.         - Len(dlgOpenFile.FileTitle) - 1)
  240.     Caption = "Bright [" & dlgOpenFile.FileTitle & "]"
  241.     ' Save the transformed image into the file.
  242.     On Error GoTo SaveError
  243.     SavePicture picResult.Picture, file_name
  244.     On Error GoTo 0
  245.     Screen.MousePointer = vbDefault
  246.     Exit Sub
  247. SaveError:
  248.     Screen.MousePointer = vbDefault
  249.     MsgBox "Error " & Format$(Err.Number) & _
  250.         " saving file '" & file_name & "'" & vbCrLf & _
  251.         Err.Description
  252. End Sub
  253.